

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.res.mac &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../search/" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../">Module code</a> &raquo;</li>
        
      <li>cup.res.mac</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.res.mac</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Authors: shouc (https://github.com/shouc)</span>
<span class="c1"># Modifier: Guannan Ma (@mythmgn)</span>
<span class="c1"># copyright:</span>
<span class="c1">#    Copyright [CUP] - See LICENSE for details.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">query mac resource module</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>

<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;get_kernel_version&#39;</span><span class="p">,</span> <span class="s1">&#39;get_cpu_nums&#39;</span><span class="p">,</span>
    <span class="s1">&#39;get_disk_usage_all&#39;</span><span class="p">,</span> <span class="s1">&#39;get_disk_info&#39;</span>
<span class="p">]</span>


<div class="viewcode-block" id="get_kernel_version"><a class="viewcode-back" href="../../../../cup.res/#cup.res.mac.get_kernel_version">[docs]</a><span class="k">def</span> <span class="nf">get_kernel_version</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    get kernel info of mac.</span>
<span class="sd">    e.g.(&#39;16&#39;, &#39;7&#39;, &#39;0&#39;):</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needmac</span>
    <span class="k">def</span> <span class="nf">_get_kernel_version</span><span class="p">():</span>
        <span class="n">versions</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">uname</span><span class="p">()[</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">([</span><span class="n">info</span> <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">versions</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)])</span>
    <span class="k">return</span> <span class="n">_get_kernel_version</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_cpu_nums"><a class="viewcode-back" href="../../../../cup.res/#cup.res.mac.get_cpu_nums">[docs]</a><span class="k">def</span> <span class="nf">get_cpu_nums</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    return cpu num</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needmac</span>
    <span class="k">def</span> <span class="nf">_get_cpu_nums</span><span class="p">():</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">sysconf</span><span class="p">(</span><span class="s2">&quot;SC_NPROCESSORS_ONLN&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">_get_cpu_nums</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_disk_usage_all"><a class="viewcode-back" href="../../../../cup.res/#cup.res.mac.get_disk_usage_all">[docs]</a><span class="k">def</span> <span class="nf">get_disk_usage_all</span><span class="p">(</span><span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param raw:</span>
<span class="sd">        measure set to Byte if Raw is True</span>
<span class="sd">    :return:</span>
<span class="sd">        a py dict: { &#39;totalSpace&#39;: xxx, &#39;usedSpace&#39;: xxx, &#39;freeSpace&#39;: xxx,</span>
<span class="sd">        &#39;unit&#39;: xxx</span>
<span class="sd">        }</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needmac</span>
    <span class="k">def</span> <span class="nf">_get_disk_usage_all</span><span class="p">(</span><span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="n">byte2gb</span> <span class="o">=</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span>
        <span class="n">byte2mb</span> <span class="o">=</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span>
        <span class="n">stat</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">statvfs</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)</span>
        <span class="n">free</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">f_bavail</span> <span class="o">*</span> <span class="n">stat</span><span class="o">.</span><span class="n">f_frsize</span>
        <span class="n">total</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">f_blocks</span> <span class="o">*</span> <span class="n">stat</span><span class="o">.</span><span class="n">f_frsize</span>
        <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;Byte&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">raw</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">total</span> <span class="o">&gt;</span> <span class="n">byte2gb</span><span class="p">:</span>
                <span class="n">free</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> \
                    <span class="n">free</span> <span class="o">/</span> <span class="n">byte2gb</span><span class="p">,</span> <span class="n">total</span> <span class="o">/</span> <span class="n">byte2gb</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;GB&quot;</span>
            <span class="k">elif</span> <span class="n">total</span> <span class="o">&gt;</span> <span class="n">byte2mb</span><span class="p">:</span>
                <span class="n">free</span><span class="p">,</span> <span class="n">total</span> <span class="o">=</span> \
                    <span class="n">free</span> <span class="o">/</span> <span class="n">byte2mb</span><span class="p">,</span> <span class="n">total</span> <span class="o">/</span> <span class="n">byte2mb</span>
                <span class="n">unit</span> <span class="o">=</span> <span class="s2">&quot;MB&quot;</span>
        <span class="k">return</span> <span class="p">{</span>
            <span class="s2">&quot;totalSpace&quot;</span><span class="p">:</span> <span class="n">total</span><span class="p">,</span>
            <span class="s2">&quot;usedSpace&quot;</span><span class="p">:</span> <span class="n">total</span> <span class="o">-</span> <span class="n">free</span><span class="p">,</span>
            <span class="s2">&quot;freeSpace&quot;</span><span class="p">:</span> <span class="n">free</span><span class="p">,</span>
            <span class="s2">&quot;unit&quot;</span><span class="p">:</span><span class="n">unit</span>
        <span class="p">}</span>
    <span class="k">return</span> <span class="n">_get_disk_usage_all</span><span class="p">(</span><span class="n">raw</span><span class="p">)</span></div>


<div class="viewcode-block" id="get_disk_info"><a class="viewcode-back" href="../../../../cup.res/#cup.res.mac.get_disk_info">[docs]</a><span class="k">def</span> <span class="nf">get_disk_info</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :return:</span>
<span class="sd">        get disk info from the current macOS</span>

<span class="sd">    :raise Exception:</span>
<span class="sd">        RuntimeError, if got no disk at all</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needmac</span>
    <span class="k">def</span> <span class="nf">_get_disk_info</span><span class="p">():</span>
        <span class="n">info</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="s2">&quot;df -lh&quot;</span><span class="p">)</span>
        <span class="n">all_diskinfo</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">readlines</span><span class="p">()):</span>
            <span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">blockinfo</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">block</span> <span class="ow">in</span> <span class="n">line</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">):</span>
                    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">block</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                        <span class="n">blockinfo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>
                <span class="n">all_diskinfo</span><span class="o">.</span><span class="n">append</span><span class="p">({</span>
                    <span class="s2">&quot;FileSystem&quot;</span><span class="p">:</span>  <span class="n">blockinfo</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                    <span class="s2">&quot;Size&quot;</span><span class="p">:</span>        <span class="n">blockinfo</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span>
                    <span class="s2">&quot;Used&quot;</span><span class="p">:</span>        <span class="n">blockinfo</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span>
                    <span class="s2">&quot;Available&quot;</span><span class="p">:</span>   <span class="n">blockinfo</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
                    <span class="s2">&quot;Percentage&quot;</span><span class="p">:</span>  <span class="n">blockinfo</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span>
                    <span class="p">})</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">continue</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">all_diskinfo</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;couldn&#39;t find disk&quot;</span><span class="p">)</span></div>


<span class="k">if</span> <span class="s1">&#39;__main__&#39;</span> <span class="o">==</span> <span class="vm">__name__</span><span class="p">:</span>
    <span class="c1"># system info</span>
    <span class="nb">print</span> <span class="n">get_cpu_nums</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_kernel_version</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_disk_usage_all</span><span class="p">()</span>
    <span class="nb">print</span> <span class="n">get_disk_info</span><span class="p">()</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>